<template>
  <div>
    <div class="obox">
      <div class="avg">
        <svg
        @click='goback()'
          t="1565833922621"
          class="icon"
          viewBox="0 0 1024 1024"
          version="1.1"
          xmlns="http://www.w3.org/2000/svg"
          p-id="4181"
          width="32"
          height="32"
        >
          <path
            d="M211.534802 512 704.372481 23.272727 798.246325 116.363636 399.282489 512 798.246325 907.636364 704.372481 1000.727272 211.534802 512Z"
            p-id="4182"
          ></path>
        </svg>
      </div>
     
      <div id="header">
        购物车
      </div>
      <div id="select_all">
        <input type="checkbox" v-model="checkAll" @change="selectAll" />全选
      </div>
    </div>
    <div class="content">
      <div style="height:40px"></div>
      <div class="fox" v-for="(item, index) in data" :key="index">
        <div class="parent">
          <input
            type="checkbox"
            :value="item.price"
            v-model="checkItem"
            @change="selectOne"
          />
        </div>
        <div>
          <img :src="item.img" alt="" width="100px" height="120" />
        </div>
        <div class="font">
          <div class="title">{{ item.shopname }}</div>
          <div class="inventory">
            <el-input-number
              v-model="item.num"
              @change="handleChange"
              :min="1"
              :max="10"
            ></el-input-number>
          </div>
          <div class="font-color">￥{{ item.price }}</div>
          <div style="height:40px"></div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="numprice">总价:{{ num }}</div>
      <div class="allprice">去结算</div>
    </div>
    </div>
  
</template>

<script type="text/javascript" src="js/vue.js"></script>
<script>
export default {
  data() {
    return {
      data: {},
      check: [],
      checkItem: [],
      checkAll: false,
      sum: {},
      num: 0,
      allprice: [],
      sid:''
    };
  },
  mounted() {
    this.fun();
  },
  methods: {
    selectOne() {
      console.log(this.checkItem);
      this.sum = [];
      this.num = 0;
      for (var i = 0; i < this.checkItem.length; i++) {
        this.sum.push(parseInt(this.checkItem[i]));
      }
      for (var i = 0; i < this.sum.length; i++) {
        this.num += this.sum[i];
      }
      console.log(this.num);

      if (this.checkItem.length == this.data.length) {
        this.checkAll = true;
      } else {
        this.checkAll = false;
      }
    },
    selectAll: function() {
      this.checkItem = [];
      this.sum = [];
      this.num = 0;
      if (this.checkAll) {
        for (var i = 0; i < this.data.length; i++) {
          this.checkItem.push(this.data[i].price);
        }
        for (var i = 0; i < this.checkItem.length; i++) {
          this.sum.push(parseInt(this.checkItem[i]));
        }
        for (var i = 0; i < this.sum.length; i++) {
          this.num += this.sum[i];
        }
        console.log(this.num);
      } else {
        this.checkItem = [];
        this.checkAll = false;
      }
      console.log(this.checkItem);
    },
    handleChange(value) {
      console.log(value);
    },
    fun() {
      let that = this;
      this.$axios
        .get("/buy", {
          params: {}
        })
        .then(function(response) {
          console.log(response.data);
          that.data = response.data;
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    goback(){
      this.sid=this.$route.query.sid;
     this.$router.push({
        path: "/detail",
        query:{
            id:this.sid
        }
      });
    }
  }
};
</script>
<style>
.avg {
  width: 10%;
  margin-left: 10px;
  margin-top: 20px;
}
.allprice {
  background-color: #ffffff;
  width: 40%;
  font-size: 30px;
  padding-top: 15px;
  background-color: #ff2a3a;
  color: #ffffff;
}
.oiu{
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.numprice {
  border: 1px solid #eeeeee;
  width: 59%;
  height: 100%;
  font-size: 30px;
  background-color: #ffffff;
  padding-top: 15px;
}
.content {
  width: 100%;
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
.bottom {
  width: 100%;
  height: 80px;
  border: 1px solid aliceblue;
  margin-left: -10px;
  z-index: 1000;

  display: flex;
  flex-direction: row;
  flex-wrap: wrap;

  position: fixed;
  bottom: -1px;
}
.title {
  text-overflow: ellipsis;
  /* display: -webkit-box; */
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}
.selall {
  float: right;
  margin-right: 20px;
}
.parent {
  display: flex;
  align-items: center;
  margin: 20px;
}
.air {
  height: 30px;
}
.obox {
  width: 100%;
  height: 60px;
  background: red;
  color: aliceblue;
  font-size: 20px;
  margin-left: -10px;

  position: fixed;
  top: 0px;
  display: block;
  z-index: 1000;
}
.fox {
  width: 100%;
  display: flex;
  flex-direction: row;
  margin-right: 20px;
  margin-top: 20px;
  border-bottom: 1px solid #efefef;
}
.font {
  display: flex;
  flex-direction: column;
  margin-left: 10%;
  flex-wrap: wrap;
  padding-top: 10px;
}
.numbox {
  margin-bottom: 10px;
}

#header {
  height: 50%;
  padding-top: 18px;
  margin-top: -55px;
  margin-left: 25%;
  width: 50%;
  float: left;
  font-size: 25px;
}

#select_all {
  width: 80px;
  padding-top: 25px;

  margin-top: -55px;
  margin-left: 80%;
}

.inventory {
  margin: 10px;
}
</style>
